package com.szt.mobiles.inventory.common.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * The class is used for saving simple key-value in a xml file.
 *
 * @author Brain
 */
public class SpUtil {
    private Context context;
    private SharedPreferences sharedPreferences = null;
    SharedPreferences.Editor editor;

    /**
     * Preferences Name that we use.
     */
    private static final String PREF_NAME = "GuoSenPDA";

    /**
     * Holds the single instance that is shared by the process.
     */
    private static SpUtil sInstance;

    /**
     * Return the single SharedPreferences instance.
     */
    public static SpUtil getInstance(Context context) {
        if (sInstance == null) {
            synchronized (PREF_NAME) {
                if (sInstance == null) {
                    sInstance = new SpUtil(context);
                    sInstance.open();
                }
            }
        }

        return sInstance;
    }

    private SpUtil() {
    }

    private SpUtil(Context context) {
        this.context = context;
    }

    private void open() {
        sharedPreferences = context.getSharedPreferences(PREF_NAME, 0);
    }

    public void close() {
        // sInstance = null;
        // sharedPreferences = null;
    }


    public boolean getKeyBooleanValue(String mKey, boolean mDefValue) {
        boolean mBool = false;
        if (sharedPreferences != null) {
            mBool = sharedPreferences.getBoolean(mKey, mDefValue);
        }
        return mBool;
    }

    /**
     * 保存boolean类型的键值对到配置文件当中.
     */
    public void saveKeyBooleanValue(String mKey, boolean mDefValue) {
        if (sharedPreferences != null) {
            editor = sharedPreferences.edit();
            editor.putBoolean(mKey, mDefValue);
            editor.apply();
        }
    }

    public void saveKeyStringValue(String mKey, String mValue) {
        if (sharedPreferences != null) {
            editor = sharedPreferences.edit();
            editor.putString(mKey, mValue);
            editor.apply();
        }
    }

    public void saveKeyLongValue(String mKey, long mValue) {
        if (sharedPreferences != null) {
            editor = sharedPreferences.edit();
            editor.putLong(mKey, mValue);
            editor.apply();
        }
    }

    public void saveKeyIntValue(String mKey, int mValue) {
        if (sharedPreferences != null) {
            editor = sharedPreferences.edit();
            editor.putInt(mKey, mValue);
            editor.apply();
        }
    }

    public String getKeyStringValue(String mKey, String mDefValue) {
        String mStr = null;
        if (sharedPreferences != null) {
            mStr = sharedPreferences.getString(mKey, mDefValue);
        }
        return mStr;
    }

    public long getKeyLongValue(String mKey, long mDefValue) {
        long mStr = mDefValue;
        if (sharedPreferences != null) {
            mStr = sharedPreferences.getLong(mKey, mDefValue);
        }
        return mStr;
    }

    public int getKeyIntValue(String mKey, int mDefValue) {
        int mStr = mDefValue;
        if (sharedPreferences != null) {
            mStr = sharedPreferences.getInt(mKey, mDefValue);
        }
        return mStr;
    }

    public void clear() {
        if (sharedPreferences != null) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.clear();
            editor.apply();
        }
    }

    public static final class Key {
        public static final String KEY_IP = "key_ip";
        public static final String KEY_PORT = "key_port";
        public static final String KEY_USERNAME = "key_username";
        public static final String KEY_PASSWORD = "key_password";
        public static final String KEY_INSPECT_ID = "key_inspect_id";
        public static final String KEY_MAX_POWER = "key_max_power";
        public static final String KEY_MIN_POWER = "key_min_power";
    }

    /****设置状态  false为安装后第一次登录，true为已经登录过****/
    public void setState() {
        SharedPreferences sp = context.getSharedPreferences("save.himi", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isLogin", true);
        editor.commit();
    }

    /***获取状态***/
    public boolean getState() {
        SharedPreferences sp = context.getSharedPreferences("save.himi", Context.MODE_PRIVATE);
        boolean b = sp.getBoolean("isLogin", false);
        return b;
    }
}
